//
//  LIURequestViewModel.m
//  RxNet
//
//  Created by liuyu on 17/12/16.
//  Copyright (c) 2016 limit. All rights reserved.
//

#import "LIURequestViewModel.h"
#import "LIUMovie.h"
#import "LIUNetUnit.h"
#import "LIUKeyValues.h"

@implementation LIURequestViewModel
- (instancetype)init {
    if (self = [super init]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    _getCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *input) {
        RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSURLSessionTask *task = [LIUNetUnit get:input
                                                exec:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                    LIUMovie *movie = [LIUMovie objectWithKeyValues:data];
                                                    [subscriber sendNext:movie];
                                                    [subscriber sendCompleted];
                                                }];
            [task resume];
            return nil;
        }];
        return signal;
    }];
    _postCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSArray *input) {
        RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSURLSessionTask *task = [LIUNetUnit post:input.firstObject
                                               params:input.lastObject
                                                 exec:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                     [subscriber sendNext:data];
                                                     [subscriber sendCompleted];
                                                 }];
            [task resume];
            return nil;
        }];
        return signal;
    }];
}

@end
